package com.xiaoluo.lottery.web;

import com.xiaoluo.lottery.entity.Order;
import com.xiaoluo.lottery.request.CreateRoundRequest;
import com.xiaoluo.lottery.request.JoinRoundRequest;
import com.xiaoluo.lottery.service.RoundService;
import com.xiaoluo.lottery.vo.CreateRoundResult;
import com.xiaoluo.lottery.vo.RoundVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * Created by Caedmon on 2016/4/6.
 */
@Controller
@RequestMapping("/round")
public class RoundController extends BaseController{
    @Autowired
    private RoundService roundService;
    @RequestMapping(value = "/create")
    public @ResponseBody String create(String createPlayerOpenId,
                                       @RequestParam( defaultValue = "0") int gameType,
                                       double singleAmount,int num,@RequestParam(required = false) Date expiredTime,String name,
                                       @RequestParam(required = false,defaultValue = "0") long itemId,String ip) throws Exception{
        CreateRoundRequest settings=new CreateRoundRequest();
        settings.setCreatePlayerOpenId(createPlayerOpenId);
        settings.setGameType(CreateRoundRequest.GameType.valueOf(gameType));
        settings.setSingleAmount(singleAmount);
        settings.setNum(num);
        settings.setExpiredTime(expiredTime);
        settings.setName(name);
        settings.setIp(ip);
        CreateRoundResult result=roundService.createRound(settings);
        return new ApiResult(result).toString();
    }
    @RequestMapping("/join")
    public @ResponseBody String join(JoinRoundRequest request){
        Order order=roundService.getPreJoinPayOrder(request);
        return new ApiResult(order.getPrepayId()).toString();
    }
    @RequestMapping("/detail")
    public @ResponseBody String getDetail(long roundId) throws Exception{
        RoundVo round=roundService.getRoundDetail(roundId);
        return new ApiResult(round).toString();
    }

}
